//
//  YFThreeDModelItem.m
//  PaddleTennisDemo
//
//  Created by Coollang on 2017/11/2.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFThreeDItem.h"
#import "NSDate+FormateString.h"

@implementation YFThreeDItem

+(NSString *)primaryKey{
    return @"identification";
}
- (void)parseThreeDItemFormDeviceDict:(NSDictionary *)dict {
    self.groupCount = [dict[@"groupCount"] integerValue];
    self.actionType = (SportPoseType)[dict[@"actionType"] integerValue];
    self.strength = [dict[@"strength"] floatValue];
    self.swingSpeed = [dict[@"swingSpeed"] integerValue];
    self.swingTime = [dict[@"swingTime"] integerValue];
    self.unixTimeStamp = [dict[@"timeStamp"] longLongValue];
    self.isHitBall = [dict[@"isHitBall"] boolValue];
    self.handBallType = (JCHandBallType)[dict[@"handBallType"] integerValue];
    self.handType = (HandDirectionType)[dict[@"handType"] integerValue];
    self.serveDirectionType = (ServeDirectionType)[dict[@"serveDirectionType"] integerValue];
    self.areaValue = [dict[@"areaValue"] integerValue];
    self.startID = [dict[@"startID"] integerValue];
    self.endID = [dict[@"endID"] integerValue];
    self.maxSpeedId = [dict[@"maxSpeedId"] integerValue];
    self.identification = [NSString stringWithFormat:@"%@_%lld",[User getLastUser].userID, self.unixTimeStamp];
    self.templateType = [YFThreeDItem handDirectionType:_handType andActionType:_actionType];
}

// 模型转数据字典
- (NSDictionary *)threeDItemToDictionary {
    NSMutableArray *arrM = [NSMutableArray array];
    for (YFThreeDDetailItem *detailItem in self.threeDDetailItems) {
        [arrM addObject:detailItem.dictionaryToThreeDView];
    }
   return @{
      @"groupCount":@(self.groupCount),
      @"actionType":@(self.actionType),
      @"swingSpeed":@(self.swingSpeed),
      @"strength":@(self.strength),
      @"swingTime":@(self.swingTime),
      @"timeStamp":@(self.unixTimeStamp),
      @"isHitBall":@(self.isHitBall),
      @"handBallType":@(self.handBallType),
      @"handType":@(self.handType),
      @"serveDirectionType":@(self.serveDirectionType),
      @"areaValue":@(self.areaValue),
      @"startID":@(self.startID),
      @"endID":@(self.endID),
      @"maxSpeedId":@(self.maxSpeedId),
      @"threeDDetailItems":arrM.copy,
     };
}

- (NSString *)dateString {
    return [NSDate timeWithTimeIntervalString:self.unixTimeStamp];
}

// 截取有效的3d数据并转成数组
- (NSArray *)getValidThreeDDetailItemsData {
    NSMutableArray *arrM = [NSMutableArray array];
    for (YFThreeDDetailItem *detailItem in self.threeDDetailItems) {
        if (detailItem.indexI >= self.startID && detailItem.indexI <= self.endID) {
             [arrM addObject:[detailItem dictionaryToThreeDView]];
        }
    }
    return arrM.copy;
}


+ (ThreeDTemplateType)handDirectionType:(HandDirectionType)handType andActionType:(SportPoseType)actionType {
    switch (actionType) {
        case SportPoseTypeShaQiu:
            return (handType == HandDirectionTypeForeward ? ThreeDTemplateTypeForehandSmash : ThreeDTemplateTypeBackhandSmash);
            break;
        case SportPoseTypeCuoQiu:
            return (handType == HandDirectionTypeForeward ? ThreeDTemplateTypeForehandShot : ThreeDTemplateTypeBackhandShot);
            break;
        case SportPoseTypeChouQiu:
            return (handType == HandDirectionTypeForeward ? ThreeDTemplateTypeForehandDrive : ThreeDTemplateTypeBackhandDrive);
            break;
        case SportPoseTypeTiaoQiu:
            return (handType == HandDirectionTypeForeward ? ThreeDTemplateTypeForehandLift : ThreeDTemplateTypeBackhandLift);
            break;
        case SportPoseTypeDangQiu:
            return (handType == HandDirectionTypeForeward ? ThreeDTemplateTypeBackhandBlock : ThreeDTemplateTypeForehandBlock);
            break;
        case SportPoseTypeDiaoQiu:
            return (handType == HandDirectionTypeForeward ? ThreeDTemplateTypeForehandDropShot : ThreeDTemplateTypeBackhandDropShot);
            break;
        case SportPoseTypeGaoQiu:
             return (handType == HandDirectionTypeForeward ? ThreeDTemplateTypeForehandServeHighClear : ThreeDTemplateTypeBackhandHighClear);
        default:
            return ThreeDTemplateTypeUnknow;
            break;
    }
    
    return ThreeDTemplateTypeUnknow;
}

- (void)disposeForeBackFaceOfRacket:(NSArray <YFThreeDDetailItem *>*)modeArray {
    CGFloat gxTemp = 0;
    for (YFThreeDDetailItem *modeItem in modeArray) {
        CGFloat gy = modeItem.gy;
        if (fabs(gy) > fabs(gxTemp)) {
            gxTemp = gy;
        }
    }
    if (gxTemp < 0) {
//        YFLog(@"【取反】 小于 0 >> %f",gxTemp);
        for (YFThreeDDetailItem *modeItem in modeArray) {
            modeItem.vx = -modeItem.vx;
            modeItem.vy = -modeItem.vy;
        }
    }else{
//        YFLog(@"【不做处理】 - 大于 0 >> %f",gxTemp);
    }
}

- (NSString *)typeString{
    switch (self.templateType) {
        case ThreeDTemplateTypeUnknow:
            return @"未知类型";
            break;
        case ThreeDTemplateTypeForehandHighClear:
            return @"正手击高远球";
            break;
        case ThreeDTemplateTypeForehandLift:
            return @"正手挑球";
            break;
        case ThreeDTemplateTypeForehandSmash:
            return @"正手扣杀";
            break;
        case ThreeDTemplateTypeForehandDrive:
            return @"正手抽球";
            break;
        case ThreeDTemplateTypeForehandBlock:
            return @"正手挡球";
            break;
        case ThreeDTemplateTypeForehandShot:
            return @"正手搓球";
            break;
        case ThreeDTemplateTypeForehandDropShot:
            return @"正手吊球";
            break;
        case ThreeDTemplateTypeForehandServeHighClear:
            return @"正手发高远球";
            break;
            
        case ThreeDTemplateTypeBackhandHighClear:
            return @"反手击高远球";
            
        case ThreeDTemplateTypeBackhandLift:
            return @"反手挑球";
            
        case ThreeDTemplateTypeBackhandSmash:
            return @"反手扣杀";
       
        case ThreeDTemplateTypeBackhandDrive:
            return @"反手抽球";
  
        case ThreeDTemplateTypeBackhandBlock:
            return @"反手挡球";
        case ThreeDTemplateTypeBackhandShot:
            return @"反手搓球";
        case ThreeDTemplateTypeBackhandDropShot:
            return @"反手吊球";
            
        default:
            break;
    }
}


@end

@implementation YFThreeDDetailItem

- (void)parseThreeDDetailItemFormDeviceDict:(NSDictionary *)dict {
   
    self.gx = [dict[@"gx"] integerValue];
    self.gy = [dict[@"gy"] integerValue];
    self.gz = [dict[@"gz"] integerValue];
    
    self.ax = [dict[@"ax"] integerValue];
    self.ay = [dict[@"ay"] integerValue];
    self.az = [dict[@"az"] integerValue];
    
    if ([dict.allKeys containsObject:@"indexI"]) {
        self.indexI = [dict[@"indexI"] integerValue];
        self.q0 = dict[@"q0"];
        self.q1 = dict[@"q1"];
        self.q2 = dict[@"q2"];
        self.q3 = dict[@"q3"];
    }else if ([dict.allKeys containsObject:@"iwitch"]){
        self.indexI = [dict[@"iwitch"] integerValue];
        self.q0 = dict[@"q1"];
        self.q1 = dict[@"q2"];
        self.q2 = dict[@"q3"];
        self.q3 = dict[@"q4"];
    }
    
    CGFloat qq0 = self.q0.floatValue;
    CGFloat qq1 = self.q1.floatValue;
    CGFloat qq2 = self.q2.floatValue;
    CGFloat qq3 = self.q3.floatValue;
    
    self.vx = (qq1 * qq3 - qq0 * qq2) * 196;
    self.vy = (qq0 * qq1 + qq2 * qq3) * 196;
    self.vz = (qq0 * qq0 - 0.5 + qq3 * qq3) * 196;
}


- (NSDictionary *)dictionaryToThreeDView {
    return @{
             @"ax": [NSString stringWithFormat:@"%ld",(long)self.ax],
             @"ay": [NSString stringWithFormat:@"%ld",(long)self.ay],
             @"az": [NSString stringWithFormat:@"%ld",(long)self.az],
             @"gx": [NSString stringWithFormat:@"%ld",(long)self.gx],
             @"gy": [NSString stringWithFormat:@"%ld",(long)self.gy],
             @"gz": [NSString stringWithFormat:@"%ld",(long)self.gz],
             @"q1": self.q0,
             @"q2": self.q1,
             @"q3": self.q2,
             @"q4": self.q3,
             @"iwitch": @(self.indexI)
             };
}

- (NSNumber *)strength{
    return @((NSInteger)(sqrt((self.ax  * self.ax + self.ay * self.ay + self.az * self.az) * 0.1)));
}

- (NSNumber *)speed{
    return @((NSInteger)(0.0495 * sqrt(self.gx * self.gx + self.gy * self.gy)));
}

@end
